package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.StringPool;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.dao.SysLogSwitchDao;
import com.ccp.dev.system.model.SysLogSwitch;
import com.ccp.dev.system.model.SysUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 日志开关 信息操作服务类
 *
 * @author zw
 * @date 2019-07-18 19:04:01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysLogSwitchService extends BaseService<SysLogSwitch> {

    @Resource
    private SysLogSwitchDao sysLogSwitchDao;

    /**
     * 重写queryList
     * @param queryFilter 条件
     * @return 日志模块名称
     */
    @Override
    public List<SysLogSwitch> queryList(QueryFilter queryFilter){

        List<SysLogSwitch> switchList = sysLogSwitchDao.queryList(new HashMap<>());
        SysAuditModelType[] types =  SysAuditModelType.values();
        for(SysAuditModelType type:types){
            SysLogSwitch logSwitch = sysLogSwitchDao.getByModel(type.toString());
            if(logSwitch ==null){
                logSwitch = getInitSysLogSwitch(type);
                switchList.add(logSwitch);
            }
        }
        return switchList;
    }

    /**
     * 如果有枚举类的模块未保存到数据，则构造默认数据
     * @param sysAuditModelType 枚举类中的一个模块
     * @return 默认数据
     */
    private SysLogSwitch getInitSysLogSwitch(SysAuditModelType sysAuditModelType){
        SysLogSwitch sysLogSwitch = new SysLogSwitch();
        sysLogSwitch.setModel(sysAuditModelType.toString());
        sysLogSwitch.setStatus(SysLogSwitch.STATUS_CLOSE);
        sysLogSwitch.setMemo(sysAuditModelType.toString());
        sysLogSwitch.setFromType(SysLogSwitch.FROM_TYPE_IN);
        return sysLogSwitch;
    }

    /**
     * 打开或关闭开关
     * @param sysLogSwitch 开关数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void switchStatus(SysLogSwitch sysLogSwitch){
        String id = sysLogSwitch.getId();
        if(StringUtil.isEmpty(id) || StringPool.NULL.equalsIgnoreCase(id)){
            id = UUIDUtils.getUUIDFor32();
            sysLogSwitch.setId(id);
            fillData(sysLogSwitch,true);
            sysLogSwitchDao.add(sysLogSwitch);
        }else{
            fillData(sysLogSwitch,false);
            sysLogSwitchDao.update(sysLogSwitch);
        }
    }

    /**
     * 填充创建人、更新人相关数据
     * @param sysLogSwitch 基础数据对象
     * @param isAdd 是否新增数据
     * @return 填充后的数据
     */
    public SysLogSwitch fillData(SysLogSwitch sysLogSwitch,boolean isAdd){
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
        Date currDate = new Date();
        if(isAdd){
            sysLogSwitch.setCreator(sysUser.getFullName());
            sysLogSwitch.setCreatorid(sysUser.getUserId());
            sysLogSwitch.setCreatetime(currDate);
        }
        sysLogSwitch.setUpdby(sysUser.getFullName());
        sysLogSwitch.setUpdbyid(sysUser.getUserId());
        sysLogSwitch.setUpdateTime(currDate);
        sysLogSwitch.setLastuptime(currDate);
        return sysLogSwitch;
    }


    public SysLogSwitch getByModel(String model){
        return sysLogSwitchDao.getByModel(model);
    }


}
